package io.renren.modules.generator.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.Hashtable;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;

import io.renren.modules.generator.entity.BsnGonggaoEntity;
import io.renren.modules.generator.service.BsnGonggaoService;
import io.renren.common.utils.ConstantLocal;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.common.utils.ShortUuidUtils;



/**
 * 快报
 *
 */
@RestController
@RequestMapping("generator/bsnKb")
public class BsnKbController {
    @Autowired
    private BsnGonggaoService bsnGonggaoService;

    /**
     * 列表
     * params参数参见Query对象
     * limit=10  //每页条数
     * page=1    //当前页索引
     * sidx=     //排序字段
     * order=asc //如何排序
     */
    @RequestMapping("/list")
    public R list(@RequestBody Map<String, Object> params){
    	params.put("sidx", "create_time");
    	params.put("order", "desc");
    	EntityWrapper<BsnGonggaoEntity> wrapper = new EntityWrapper<BsnGonggaoEntity>();
    	
    	wrapper.eq("type", ConstantLocal.GONGGAO_TYPE_KB);
        PageUtils page = bsnGonggaoService.queryPage(params, wrapper);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") String id){
		BsnGonggaoEntity bsnGonggao = bsnGonggaoService.selectById(id);
        return R.ok().put("bsnGonggao", bsnGonggao);
    }

    /**
     * 信息
     */
    @RequestMapping("/lastKb")
    public R lastKb(){
    	Map<String, Object> params = new Hashtable<String, Object>();
    	params.put("page", "1");
    	params.put("limit", "10");
    	params.put("sidx", "create_time");
    	params.put("order", "desc");
    	EntityWrapper<BsnGonggaoEntity> wrapper = new EntityWrapper<BsnGonggaoEntity>();
    	
    	wrapper.eq("type", ConstantLocal.GONGGAO_TYPE_KB);
        PageUtils page = bsnGonggaoService.queryPage(params, wrapper);
        if (page.getList().size() == 0) {
        	return R.ok().put("kb", "");
        } else {
        	BsnGonggaoEntity one = (BsnGonggaoEntity)page.getList().get(0);
        	return R.ok().put("kb", one.getBiaoti());
        }
    }
    
    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody BsnGonggaoEntity bsnGonggao){
    	if (bsnGonggao == null || StringUtils.isBlank(bsnGonggao.getBiaoti())) {
    		return R.ok();
    	}
    	bsnGonggao.setId(ShortUuidUtils.generateShortUuid());
    	bsnGonggao.setType(ConstantLocal.GONGGAO_TYPE_KB);
    	bsnGonggao.setCreateTime(new Date());
		bsnGonggaoService.insert(bsnGonggao);
    	return R.ok().put("bsnGonggao", bsnGonggao);
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody BsnGonggaoEntity bsnGonggao){
    	
    	bsnGonggao.setCreateTime(new Date());
		bsnGonggaoService.updateById(bsnGonggao);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody String[] ids){
			bsnGonggaoService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }
}
